#include <Windows.h>
#include <tchar.h>
#include <assert.h>

//////////////////////////////////////////////////////////////////////////
BOOL CALLBACK WndEnumProc(HWND hWnd, LPARAM lParam)
{
	HWND hListbox = (HWND)lParam;
	if (NULL == hWnd)	return FALSE;

	if (NULL != hListbox && IsWindow(hListbox) && IsWindowVisible(hWnd))
	{
		TCHAR szWndInfo[512]	= {0};
		TCHAR szWndTitle[256]	= {0};
		TCHAR szClsName[64]		= {0};
		WINDOWPLACEMENT wp		= {0};
		wp.flags				= 0;
		wp.length				= sizeof(wp);

		RECT rtWnd, rtClient;
		GetWindowRect(hWnd, &rtWnd);
		GetClientRect(hWnd, &rtClient);
		GetWindowText(hWnd, szWndTitle, 256);
		GetClassName(hWnd, szClsName, 64);
		GetWindowPlacement(hWnd, &wp);
 		_stprintf(szWndInfo,
				  _T("\"%s\"")
				  _T("  %d ")
				  _T("wnd[%d,%d,%d,%d], client[%d,%d,%d,%d], normal[%d,%d,%d,%d] min[%d, %d] max[%d, %d]"),
				  szWndTitle,
				  wp.showCmd,
				  rtWnd.left, rtWnd.top, rtWnd.right, rtWnd.bottom,
				  rtClient.left, rtClient.top, rtClient.right, rtClient.bottom,
				  wp.rcNormalPosition.left, wp.rcNormalPosition.top, wp.rcNormalPosition.right, wp.rcNormalPosition.bottom,
				  wp.ptMinPosition.x, wp.ptMinPosition.y,
				  wp.ptMaxPosition.x, wp.ptMaxPosition.y);
		SendMessage(hListbox, LB_ADDSTRING, 0, (LPARAM)szWndInfo);
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT IDC_LSTWNDLIST	= 1003;
	static const UINT IDC_BTNDATAREAD	= 1002;

	switch (nMsg)
	{
		case WM_CREATE:
		{
			//--: Move Main wnd to center of screen
			RECT rtMainWnd;
			GetClientRect(hWnd, &rtMainWnd);
			UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
			UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
			UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
			UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
			UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
			UINT nTop			= (nSrnHeight - nWndHeight) / 2;
			MoveWindow(hWnd, nLeft, nTop, nWndWidth, nWndHeight, TRUE);
			
			CreateWindow(_T("Button"), _T("Extract Top-level Window"), WS_CHILD|WS_VISIBLE, 10, 0, 200, 50, hWnd, (HMENU)IDC_BTNDATAREAD, NULL, NULL);
			CreateWindowEx(WS_EX_CLIENTEDGE, _T("Listbox"), _T(""), WS_CHILD|WS_VISIBLE|LBS_HASSTRINGS|WS_VSCROLL, 10, 60, 970, 360, hWnd, (HMENU)IDC_LSTWNDLIST, NULL, NULL);

			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case IDC_BTNDATAREAD:
				{
					HWND hListbox	= GetDlgItem(hWnd, IDC_LSTWNDLIST);
					SendMessage(hListbox, LB_RESETCONTENT, 0, 0);
					
					EnumChildWindows(NULL, WndEnumProc, (LPARAM)GetDlgItem(hWnd, IDC_LSTWNDLIST));
					
					SetWindowText(GetDlgItem(hWnd, IDC_BTNDATAREAD), _T("Refresh"));
				}
				break;
			}
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
								0, 0, 1000, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo0035");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}